package commons;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;


public class Utility
{

	public static byte[] FileAsByteArrayExample( File file ) throws FileNotFoundException, UnsupportedEncodingException, IOException
	{
		FileInputStream fileInputStream = new FileInputStream( file );
		byte[] data = new byte[ ( int ) file.length() ];
		fileInputStream.read( data );
		fileInputStream.close();

		return data;
	}

	public static String ByteArrayToString( byte[] b )
	{
		String str = new String( b );
		return str;
	}

	/**
	 * Convert hashtable with compression parameters to properties
	 * 
	 * @param table
	 * @return
	 */
	public static Properties convertHashtableToProperties( Hashtable<String, String> table )
	{
		Properties props = new Properties();
		Enumeration<String> e = table.keys();

		while ( e.hasMoreElements() )
		{
			String element = e.nextElement();
			props.setProperty( element, table.get( element ) );
		}

		return props;
	}

	/**
	 * Convert properties with compression parameters to hashtable
	 * 
	 * @param props
	 * @return
	 */
	public static Hashtable<String, String> convertHashtableToProperties( Properties props )
	{
		Hashtable<String, String> huffmanTable = new Hashtable<String, String>();
		Enumeration<Object> e = props.keys();

		while ( e.hasMoreElements() )
		{
			String element = ( String ) e.nextElement();
			huffmanTable.put( element, ( String ) props.get( element ) );
		}

		return huffmanTable;
	}
}
